//
//  SEGWinRecordDetailTopInfoCell.m
//  ExcellentHome
//
//  Created by 揭婧雯 on 2017/8/21.
//  Copyright © 2017年 陈区. All rights reserved.
//

#import "SEGWinRecordDetailTopInfoCell.h"

@interface SEGWinRecordDetailTopInfoCell ()<UITextFieldDelegate>
/**
 描述标签
 */
@property (nonatomic, strong)UIImageView *iconView;

/**
 内容
 */
@property (nonatomic, strong)UILabel *contentLabel;

/**
 输入手机号视图
 */
@property (nonatomic, retain) UITextField *phoneText;

/**
 提交按钮
 */
@property (nonatomic, strong)UIButton *commitButton;

/**
 获取奖品按钮
 */
@property (nonatomic, strong)UIButton *getPrizeButton;

@property (nonatomic, strong) UIImageView *invaildImageView;

@end

@implementation SEGWinRecordDetailTopInfoCell

- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier {
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        self.selectionStyle = UITableViewCellSelectionStyleNone;
        [self setupUI];
    }
    return self;
}

- (void)setupUI {
    self.backgroundColor = kColor_W;
    self.userInteractionEnabled = YES;
    self.contentView.userInteractionEnabled = YES;
    
    _iconView = [[UIImageView alloc] init];
    [self.contentView addSubview:_iconView];

    _contentLabel = [[UILabel alloc] init];
    _contentLabel.font = kFont_13;
    _contentLabel.textColor = kColor_1;
    _contentLabel.numberOfLines = 0;
    _contentLabel.textAlignment = NSTextAlignmentCenter;
    [self.contentView addSubview:_contentLabel];
}

- (void)setUpCellWithModel:(SEGWinRecordDetailModel *)model {
    SEGWinReordPrizeType prizeType = [model.prizeType integerValue];
    switch (prizeType) {
        case SEGWinReordPrizeType_PhoneBill://话费
        {
            if ([model.sendStatus isEqualToString:@"1"]) {//已发放
                [self seg_showSendViewWithText:@"您的话费已经充值，请注意查收！因可能会遇上\n充值高峰期，话费到账会存在一定的延时，请谅解！"];
            }else {
                if (model.rechargeTel.length > 0) {//有电话号码
                    [self.phoneText removeFromSuperview];
                    [self.commitButton removeFromSuperview];
                    [self seg_showNotSendViewWitfhPicName:@"pic_phone" text:[NSString stringWithFormat:@"您要充值的手机号码%@已经提交成功\n话费将在活动结束后2个工作日内充值到账，请注意查收！",model.rechargeTel]];
                }else {//还没有填写电话号码
                    [self seg_showEnterPhoneView];
                    // 是否需要展示失效
                    if ([model isInvaildState]) {
                        self.phoneText.enabled = NO;
                        self.commitButton.userInteractionEnabled = NO;
                        self.commitButton.enabled = NO;
                        self.commitButton.backgroundColor = kColor_L2;
                        [self.invaildImageView mas_makeConstraints:^(MASConstraintMaker *make) {
                            make.size.mas_equalTo(CGSizeMake(67, 67));
                            make.trailing.equalTo(self.contentView.mas_trailing).offset(-25);
                            make.top.equalTo(self.phoneText.mas_top);
                        }];
                    }
                }
            }
            break;
        }
        case SEGWinReordPrizeType_Coin://积分
        {
            [self seg_showSendViewWithText:@"您的积分已发放，请注意查收！"];
            break;
        }
        case SEGWinReordPrizeType_RechargeCard://充值卡
        {
            if ([model.sendStatus isEqualToString:@"1"]) {//已发放
                [self seg_showSendViewWithText:[NSString stringWithFormat:@"您获得的%@已经发放！",model.prizeName]];
            }else {
                [self seg_showNotSendViewWitfhPicName:@"pic_card" text:[NSString stringWithFormat:@"您获得的%@将在2个工作日内发送\n请注意查收",model.prizeName]];
            }
            break;
        }
        case SEGWinReordPrizeType_OnLine://线上实物
        {
            [self seg_showOnLineView];
            // 是否需要展示失效
            if ([model isInvaildState]) {
                self.getPrizeButton.userInteractionEnabled = NO;
                self.getPrizeButton.backgroundColor = kColor_L2;
                self.getPrizeButton.enabled = NO;
                [self.invaildImageView mas_makeConstraints:^(MASConstraintMaker *make) {
                    make.size.mas_equalTo(CGSizeMake(67, 67));
                    make.trailing.equalTo(self.contentView.mas_trailing).offset(-25);
                    make.bottom.equalTo(self.getPrizeButton.mas_top).offset(10);
                }];
            }
            break;
        }
        case SEGWinReordPrizeType_OffLine://线下实物
        {
            break;
        }
    }
}

- (void)seg_showSendViewWithText:(NSString *)text {
    _iconView.image = [UIImage imageWithContentsOfFile:SEGPicName(@"pic_finish")];
    [_iconView mas_remakeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.contentView).offset(40);
        make.centerX.equalTo(self.contentView);
    }];
    
    _contentLabel.text = text;
    [_contentLabel mas_remakeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(_iconView.mas_bottom).offset(15);
        make.centerX.equalTo(self.contentView);
        make.bottom.equalTo(self.contentView).offset(-40);
    }];
}

- (void)seg_showNotSendViewWitfhPicName:(NSString *)picName text:(NSString *)text {
    _iconView.image = [UIImage imageNamed:SEGPicName(picName)];
    [_iconView mas_remakeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.contentView).offset(35);
        make.centerX.equalTo(self.contentView);
    }];
    
    _contentLabel.text = text;
    [_contentLabel mas_remakeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(_iconView.mas_bottom).offset(15);
        make.centerX.equalTo(self.contentView);
        make.bottom.equalTo(self.contentView).offset(-30);
    }];
}

/// 显示输入手机号界面
- (void)seg_showEnterPhoneView {
    _iconView.image = [UIImage imageWithContentsOfFile:SEGPicName(@"pic_phone")];
    [_iconView mas_remakeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.contentView).offset(35);
        make.centerX.equalTo(self.contentView);
    }];
    
    self.phoneText = [[UITextField alloc] init];
    self.phoneText.backgroundColor = [UIColor colorWithRed:245 / 255.0f green:245 / 255.0f blue:245 / 255.0f alpha:1.0f];
    [self.phoneText setTextColor:kColor_3];
    [self.phoneText setFont:[UIFont systemFontOfSize:13]];
    [self.phoneText setAttributedPlaceholder:[[NSAttributedString alloc] initWithString:@"请输入需要充值的手机号码" attributes:@{NSForegroundColorAttributeName:kColor_3}]];
    self.phoneText.layer.cornerRadius = 2;
    self.phoneText.layer.masksToBounds = YES;
    self.phoneText.leftView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 15, 88)];
    self.phoneText.leftViewMode = UITextFieldViewModeAlways;
    self.phoneText.delegate = self;
    self.phoneText.keyboardType = UIKeyboardTypeNumberPad;
    [self.contentView addSubview:self.phoneText];
    [self.phoneText mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(_iconView.mas_bottom).with.offset(25);
        make.left.equalTo(self.contentView).with.offset(15);
        make.right.equalTo(self.contentView).with.offset(-15);
        make.height.mas_equalTo(44);
    }];
    
    UIButton *commitButton = [[UIButton alloc] init];
    commitButton.backgroundColor = kColor_T;
    [commitButton setBackgroundImage:[UIImage imageWithColor:kColor_T] forState:UIControlStateNormal];
    [commitButton setBackgroundImage:[UIImage imageWithColor:kColor_T_Press] forState:UIControlStateHighlighted];
    [commitButton setBackgroundImage:[UIImage imageWithColor:kColor_L2] forState:UIControlStateDisabled];
    [commitButton setTitle:@"提交" forState:UIControlStateNormal];
    commitButton.titleLabel.font = [UIFont systemFontOfSize:16];
    [commitButton setTitleColor:kColor_W forState:UIControlStateNormal];
    commitButton.layer.masksToBounds = YES;
    commitButton.layer.cornerRadius = 2;
    [commitButton addTarget:self action:@selector(seg_commitButtonClick) forControlEvents:UIControlEventTouchUpInside];
    [self.contentView addSubview:commitButton];
    [commitButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.contentView).with.offset(15);
        make.right.equalTo(self.contentView).with.offset(-15);
        make.top.equalTo(self.phoneText.mas_bottom).with.offset(15);
        make.height.mas_equalTo(44);
        make.bottom.equalTo(self.contentView).offset(-30);
    }];
    self.commitButton = commitButton;
}

- (void)seg_showOnLineView {
    _iconView.image = [UIImage imageWithContentsOfFile:SEGPicName(@"pic_gift")];
    [_iconView mas_remakeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.contentView).offset(35);
        make.centerX.equalTo(self.contentView);
    }];
    
    UIButton *getPrizeButton = [[UIButton alloc] init];
    getPrizeButton.backgroundColor = kColor_T;
    [getPrizeButton setBackgroundImage:[UIImage imageWithColor:kColor_T] forState:UIControlStateNormal];
    [getPrizeButton setBackgroundImage:[UIImage imageWithColor:kColor_T_Press] forState:UIControlStateHighlighted];
    [getPrizeButton setBackgroundImage:[UIImage imageWithColor:kColor_L2] forState:UIControlStateDisabled];
    [getPrizeButton setTitle:@"点击领取奖品" forState:UIControlStateNormal];
    getPrizeButton.titleLabel.font = [UIFont systemFontOfSize:16];
    [getPrizeButton setTitleColor:kColor_W forState:UIControlStateNormal];
    getPrizeButton.layer.masksToBounds = YES;
    getPrizeButton.layer.cornerRadius = 2;
    [getPrizeButton addTarget:self action:@selector(seg_getPrizeButtonClick) forControlEvents:UIControlEventTouchUpInside];
    [self.contentView addSubview:getPrizeButton];
    [getPrizeButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.contentView).with.offset(15);
        make.right.equalTo(self.contentView).with.offset(-15);
        make.top.equalTo(_iconView.mas_bottom).with.offset(15);
        make.height.mas_equalTo(44);
        make.bottom.equalTo(self.contentView).offset(-30);
    }];
    self.getPrizeButton = getPrizeButton;
}

//限制UITextField输入长度
//最大输入长度为11个字符
- (BOOL)textField:(UITextField*)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString*)string {
    if (string.length == 0)
        return YES;
    
    NSInteger existedLength = textField.text.length;
    NSInteger selectedLength = range.length;
    NSInteger replaceLength = string.length;
    if (existedLength - selectedLength + replaceLength > 11) {
        return NO;
    }else {
        return YES;
    }
}
- (BOOL)textFieldShouldReturn:(UITextField*)textField {
    [self seg_commitButtonClick];
    return YES;
}

//提交手机号
- (void)seg_commitButtonClick {
    [self.phoneText resignFirstResponder];
    if (self.phoneText.text.length != 11) {
        [SEGAlertView showTmepMessageWithMessage:kPhoneError];
    }else {
        if (self.delegate && [self.delegate respondsToSelector:@selector(submitRechangePhone:)]) {
            [self.delegate submitRechangePhone:self.phoneText.text];
        }
    }
}

- (void)seg_getPrizeButtonClick {
    if (self.delegate && [self.delegate respondsToSelector:@selector(getPrizeAction)]) {
        [self.delegate getPrizeAction];
    }
}

- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event {
    [self.phoneText resignFirstResponder];
}

- (UIImageView *)invaildImageView {
    if (!_invaildImageView) {
        _invaildImageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:SEGPicName(@"img_failure")]];
        _invaildImageView.contentMode = UIViewContentModeCenter;
        [self.contentView addSubview:_invaildImageView];
    }
    return _invaildImageView;
}

@end
